package com.ljx.funchat.common.event.listener;

import com.ljx.funchat.common.event.UserApplyEvent;
import com.ljx.funchat.transaction.config.RabbitMQConfig;
import com.ljx.funchat.user.dao.UserApplyDao;
import com.ljx.funchat.user.domain.entity.UserApply;
import com.ljx.funchat.user.service.impl.PushService;
import com.ljx.funchat.websocket.domain.vo.resp.WSApplyResp;
import com.ljx.funchat.user.service.adatper.UserApplyAdapter;
import com.ljx.funchat.websocket.service.adatper.WebSocketAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 用户申请监听器
 */
@Slf4j
@Component
public class UserApplyListener {

    @Autowired
    private UserApplyDao userApplyDao;

    @Autowired
    private PushService pushService;

    @Async
    @EventListener(classes = UserApplyEvent.class)
    public void sendMsg(UserApplyEvent event) {
        UserApply userApply = event.getUserApply();
        // 消息未读数
        Integer unReadCount = userApplyDao.getUnReadCount(userApply.getTargetId());
        WSApplyResp wsApplyResp = UserApplyAdapter.buildApplyWsResp(userApply, unReadCount);

        pushService.sendPushMsg(RabbitMQConfig.PUSH_QUEUE, WebSocketAdapter.buildApplyResp(wsApplyResp), Arrays.asList(userApply.getTargetId()));
    }

}
